/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jdbc;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javax.sql.DataSource;

/**
 *
 * @author ADMIN
 */
public class DBConnection
{

    private static DataSource dataSource;
    private static String DRIVER_NAME;
    private static String URL;
    private static String UNAME;
    private static String PWD;

    static
    {

//          final ResourceBundle config = ResourceBundle.getBundle("props.database");
//        DRIVER_NAME = config.getString("driverName");
//        URL = config.getString("url");
//        UNAME = config.getString("uname");
//        PWD = config.getString("pwd");
        DRIVER_NAME = "com.mysql.jdbc.Driver";
        URL = "jdbc:mysql://localhost:3306/nhatnhiba";
        UNAME = "root";
        PWD = "123456";

        dataSource = setupDataSource();
    }

    public static Connection getOracleConnection() throws SQLException
    {
        return dataSource.getConnection();
    }

    private static DataSource setupDataSource()
    {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        try
        {
            cpds.setDriverClass(DRIVER_NAME);
        } catch (PropertyVetoException e)
        {
            e.printStackTrace();
        }
        cpds.setJdbcUrl(URL);
        cpds.setUser(UNAME);
        cpds.setPassword(PWD);
        cpds.setMinPoolSize(5);
        cpds.setAcquireIncrement(5);
        cpds.setMaxPoolSize(20);
        return cpds;
    }

    public static void main(String[] args) throws SQLException
    {
        Connection connect = DBConnection.getOracleConnection();
        Statement stmt = null;
        ResultSet rs = null;        
        stmt = connect.createStatement();
        rs = stmt.executeQuery("SELECT * FROM tableuser");
        while (rs.next())
        {
            System.out.println(rs.getString("UserName"));
        }

    }
}
